<?php
class Pages extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->Model(array("Mpages", "Marticle", "Mgallery"));
	}
	
	public function index(){
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_pages'),base_url().'admincp/pages');
		if($this->input->is_ajax_request()){
            $output = $this->Mpages->listPages();
            $this->output->set_output(json_encode($output));
        }else{
            $data['template']="admincp/pages/view";
            $data['title_page']='<i class="icon20 i-list-4"></i> '.trans('title_pages');
			$this->load->view("admincp/template",$data);
        }
	}

	public function add(){
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_pages'),base_url().'admincp/pages');
		$this->breadcrumb->append_crumb(trans('breadcrumb_pages_add'),base_url().'admincp/pages/add');
		$data['template']="pages/add";
		$data['title_page']='<i class="icon20 i-plus-circle-2"></i> '.trans('title_pages_add');
		$data['article']=$this->Marticle->list_all();
		$data['gallery']=$this->Mgallery->list_all(3);
		$path_upload = 'assets/filemanager/background/';
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$allowed_ext = array('jpg','jpeg','png','gif');
			$dataInsertImage = array();
			if(array_key_exists('background',$_FILES) && $_FILES['background']['error'] == 0 ){
            
	            $pic = $_FILES['background'];
	
	            if(!in_array(get_extension($pic['name']),$allowed_ext)){
	                exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
	            }            
	            
	            // Move the uploaded file from the temporary 
	            // directory to the uploads folder:

	            if(move_uploaded_file($pic['tmp_name'], './'.$path_upload.$pic['name'])){

					$dataInsertImage = array(
						'background' => $path_upload.$pic['name']
					);
	            }
	        }

	        if(isset($_POST['switch_gallery']) && $_POST['switch_gallery'] != ''){
	        	$active = 1;
	        }else{
	        	$active = 0;
	        }

			$data_insert = array(
				'page_name' => $this->input->post('page_name'),
				'page_name_large' => $this->input->post('page_name_large'),
				'page_key' => $this->input->post('page_key'),
				'page_description' => $this->input->post('page_description'),
				'page_url' => $this->input->post('page_url'),
				'art_id' => $this->input->post('art_id'),
				'active_gallery' => $active,
				'gallery_id' => $this->input->post('gallery_id'),
				'order' => $this->input->post('order'),
				'date_add' => date('Y-m-d',time()), 
			);

			$dataInsertFinal = array_merge($data_insert, $dataInsertImage);

			$this->Mpages->insert($dataInsertFinal);

			$this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
			redirect(base_url()."admincp/pages");		
		}
		$this->load->view("admincp/template",$data);
	}

	public function edit(){
		$id = $this->uri->segment(5);
		$this->breadcrumb->append_crumb(trans('breadcrumb_home'),admincp_url());
		$this->breadcrumb->append_crumb(trans('breadcrumb_pages'),base_url().'admincp/pages');
		$this->breadcrumb->append_crumb(trans('breadcrumb_pages_edit'),base_url().'admincp/pages/edit/id/'.$id);
		$data['template']="pages/edit";
		$data['title_page']='<i class="icon20 i-pen-2"></i> '.trans('title_pages_edit');
		$data['article']=$this->Marticle->list_all();
		$data['gallery']=$this->Mgallery->list_all(3);
		$data['info'] = $this->Mpages->get_data($id);
		$path_upload = 'assets/filemanager/background/';
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$allowed_ext = array('jpg','jpeg','png','gif');
			$dataUpdateImage = array();
			if(array_key_exists('background',$_FILES) && $_FILES['background']['error'] == 0 ){
            
	            $pic = $_FILES['background'];
	
	            if(!in_array(get_extension($pic['name']),$allowed_ext)){
	                exit_status('Only '.implode(',',$allowed_ext).' files are allowed!');
	            }            
	            
	            // Move the uploaded file from the temporary 
	            // directory to the uploads folder:

	            if(move_uploaded_file($pic['tmp_name'], './'.$path_upload.$pic['name'])){

					$dataUpdateImage = array(
						'background' => $path_upload.$pic['name'],
					);
	            }
	            
	        }

	        if(isset($_POST['switch_gallery']) && $_POST['switch_gallery'] != ''){
	        	$active = 1;
	        }else{
	        	$active = 0;
	        }

			$dataUpdate = array(
				'page_name' => $this->input->post('page_name'),
				'page_name_large' => $this->input->post('page_name_large'),
				'page_key' => $this->input->post('page_key'),
				'page_description' => $this->input->post('page_description'),
				'page_url' => $this->input->post('page_url'),
				'art_id' => $this->input->post('art_id'),
				'active_gallery' => $active,
				'gallery_id' => $this->input->post('gallery_id'),
				'order' => $this->input->post('order'),
				'date_edit' => date('Y-m-d',time()), 
			);

			$dataUpdateFinal = array_merge($dataUpdate,$dataUpdateImage);

			$this->Mpages->update($dataUpdateFinal,$id);

			$encry = md5(base64_encode($this->input->post('page_url').$id));
			$this->cache->delete($encry);
			$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
			redirect(base_url()."admincp/pages");
		}
		$this->load->view("admincp/template",$data);
	}

	public function delete(){
		$id = $this->uri->segment(5);
		$this->Mpages->delete($id);
		$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		redirect(base_url()."admincp/pages");
	}
	
	public function check_name($name,$sec_id){
		if($this->Mpages->check_name($name, $sec_id) == FALSE){
			return FALSE;
		}else{
			return TRUE;
		}
	}
}